// console.log(window.location.href);
// const param = window.location.href.match(/\?([^&]*([=][^&]*)?&)*id=([^&]*)&?/);
// console.log(param);
// if (!param) {
//   alert("作者不存在");
//   window.history.back();
// }
// const userID = param[3];

const requestParams = HTTP.getRequestParams();
const userID = requestParams["id"];
if (!userID) {
  alert("作者不存在");
  window.history.back();
}

(async function () {
  "use strict";

  let configuration = {};
  await Promise.all([WebsiteConfigurationPromise]).then((arr) => {
    configuration = arr[0];
  });

  let userInfo = null;

  const getUserResponse = await HTTP.request({
    method: "GET",
    url: `/api/user/id/${userID}`,
  });
  if (getUserResponse.data.type === "failure") {
    console.error(getUserResponse.data.message);
    alert("错误");
    window.history.back();
    return;
  }
  userInfo = getUserResponse.data.data;
  document.title = "作者——" + userInfo.nickname;

  const description = configuration.websiteName + document.title;
  document.querySelector("meta[name='description']").content = description;

  document.querySelector("meta[property='og:title']").content = document.title;
  document.querySelector("meta[property='og:url']").content =
    `http://cyue.net/pages/author.html?id=${userInfo.id}`;
  document.querySelector("meta[property='og:description']").content =
    description;

  document.querySelector("meta[name='twitter:title']").content = document.title;
  document.querySelector("meta[name='twitter:description']").content =
    description;
  document.querySelector("meta[name='twitter:creator']").content =
    `@${userInfo.nickname}`;

  const { createApp } = Vue;
  createApp({
    mounted() {
      const avatarIMG = this.$refs.avatarIMG;
      // HTTP.request({
      //   method: "GET",
      //   url: `/api/avatar/${this.user.id}`,
      // }).then((response) => {
      //   const result = response.data;
      //   avatarIMG.src = result;
      // });
      // avatarIMG.src = this.user.avatar
      //   ? this.user.avatar
      //   : "/images/image.error.png";
      avatarIMG.src = `/avatar/${this.user.id}`;

      HTTP.request({
        method: "GET",
        url: `/api/article/sort/all`,
      }).then(
        ((response) => {
          const result = response.data;
          this.sortDataSource = result.data;
        }).bind(this),
      );

      HTTP.request({
        method: "GET",
        url: `/api/article/user/id/${this.user.id}`,
      }).then(
        ((response) => {
          const result = response.data;
          for (let index = 0; index < result.data.length; index++) {
            const element = result.data[index];
            if (!this.articleDataSource[element.sort])
              this.articleDataSource[element.sort] = [];
            this.articleDataSource[element.sort].push(element);
          }
        }).bind(this),
      );
    },

    data() {
      return {
        user: userInfo,
        sortDataSource: [],
        articleDataSource: {},
      };
    },

    methods: {},
  }).mount("#app");
})();
